// pages/login/index.js
import axios from "../network/index"
let timer = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: "",
    passowrd: "",
    countDown: 60,
  },

  signIn() {
    wx.showModal({
      title: "温馨提示",
      content: "暂未对外开放，目前只提供内部人员使用！",
      showCancel: false
    })
    // wx.navigateTo({
    //   url: '/pages/signIn/index',
    // })
  },
  //检测是否需要实名认证方法
  async checkAuthFnc(userId) {
    return axios({
      url: "/api/user/userDetail",
      method: 'post',
      data: {
        userId
      }
    }).then(res => {
      if (res.code == 200) {
        return res
      }
    })
  },
  //检测实名
  checkAuthentication(userId) {
    let _this = this
    _this.checkAuthFnc(userId).then(res => {
      console.log(res, '监测信息');
      // 实名认证-----------------------------------
      // if (!res.data.idCardNumber) {
        // wx.reLaunch({
        //   url: '/pages/signIn/index',
        // })
      // } else {
        // 实名认证-----------------------------------
        wx.requestSubscribeMessage({
          tmplIds: ['bfS_BkB33IJaL6gTBCH92kO4IzWE21W0MKM6FhsAM-k', 'Hwp9R63X_HWdFe_dl8H7Y7zq5inEcePKScZgq-NA3j4'],
          success(v) {
            if (v.subscriptionsSetting) {
              wx.showToast({
                title: '订阅成功！',
              })
            }
          },
          fail(v) {
            wx.showToast({
              title: '订阅失败！',
              icon: "none"
            })
          }
        })
        wx.setStorageSync('isAuthentication', 1)
        wx.reLaunch({
          url: '/pages/detail/lindex',
        })
      // }实名认证
    })
  },
  //登录
  login() {
    let _this = this
    wx.getSetting({
      withSubscriptions: true,
      success(v) {
        console.log(v)
      }
    })

    if (this.data.username == '' || this.data.passowrd == '') {
      wx.showToast({
        title: '用户或者密码不能为空',
        icon: "none"
      })
      return
    }
    wx.showLoading({
      title: '登录中',
      mask: true
    })

    axios({
      url: "/api/login/loginCheck",
      data: {
        pwd: this.data.passowrd,
        tel: this.data.username
      }
    }).then(v => {
      wx.hideLoading()
      if (v.code == 200) {
        wx.setStorageSync('tel', this.data.username)
        wx.setStorageSync('userId', v.data.userId)
        wx.setStorageSync('name', v.data.name)
        wx.setStorageSync('icon', v.data.icon)
        wx.setStorageSync('positionName', v.data.positionName)
        _this.checkAuthentication(v.data.userId)
      } else {
        wx.showToast({
          title: v.msg,
          icon: 'none',
          duration: 1500
        })
      }
    }).catch(() => {
      wx.hideLoading()
    })
  },
  //自动登录
  selfAction(tel) {
    let _this = this
    axios({
      url: "/api/login/autoLoginCheck",
      data: {
        tel
      }
    }).then(v => {
      if (v.code) {
        wx.setStorageSync('userId', v.data.userId)
        wx.setStorageSync('name', v.data.name)
        wx.setStorageSync('icon', v.data.icon)
        wx.setStorageSync('positionName', v.data.positionName)
        _this.checkAuthentication(v.data.userId)
      } else {
        wx.showToast({
          icon: "none",
          title: '自动登录失败,重新登录',
          success: function () {
            wx.removeStorageSync('tel')
            wx.navigateTo({
              url: '/pages/login/index',
            })
          }
        })
      }
    })
  },
  username(e) {

    this.setData({
      username: e.detail.value
    })
  },
  getCode() {
    if (!(this.data.countDown == 60 || this.data.countDown == 0)) {
      wx.showToast({
        icon: 'none',
        title: "请在" + this.data.countDown + "秒重试",
      })
      return
    }
    let tel = Number(this.data.username)
    if (this.data.tel == '') {
      wx.showToast({
        icon: "none",
        title: '手机号码不能为空',
      })
    } else if (!(/^1[3456789]\d{9}$/.test(tel))) {
      wx.showToast({
        icon: "none",
        title: '手机号有误，请重新填写',
      })
    } else {
      wx.showLoading({
        title: '发送中',
        mask: true
      })
      axios({
        url: '/api/login/loginVerificationCode',
        data: {
          tel
        }
      }).then(v => {
        wx.hideLoading()
        if (v.code == 200) {
          wx.showToast({
            title: '发送成功！',
            duration: 1500,
          })
          this.setData({
            countDown: this.data.countDown - 1
          })
          timer = setInterval(v => {
            let count = this.data.countDown
            count -= 1
            this.setData({
              countDown: count
            })

            if (this.data.countDown < 1) {
              clearInterval(timer)
              this.setData({
                countDown: 60
              })
            }
          }, 1000)

        } else {
          wx.showToast({
            icon: 'none',
            title: v.msg,
          })
        }
      }).catch(() => {
        wx.hideLoading()

      })

    }
  },
  passowrd(e) {
    this.setData({
      passowrd: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.hideHomeButton()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let tel = wx.getStorageSync('tel')
    if (tel != '') {
      wx.showToast({
        title: '自动登录',
      })
      this.selfAction(tel)
    } else {
      return
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})